package com.example.shoppingmall.community.fragment;

import android.util.Log;
import android.view.View;
import android.widget.ListView;

import com.example.shoppingmall.R;
import com.example.shoppingmall.base.BaseFragment;
import com.example.shoppingmall.community.bean.HotPostBean;
import com.example.shoppingmall.utils.Constants;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import okhttp3.Call;

public class HotPostFragment extends BaseFragment {
    private ListView lv_hot_post;
    private List<HotPostBean.ResultDTO> result;
    @Override
    public View initView() {
        View view = View.inflate(mContext, R.layout.fragment_hot_post,null);
        lv_hot_post = (ListView) view.findViewById(R.id.lv_hot_post);
        return view;
    }
    
    @Override
    public void initData(){
        getDataFromNet();
    }

    private void getDataFromNet() {
        OkHttpUtils.
                get().
                url(Constants.HOT_POST_URL)
                .id(100)
                .build()
                .execute(new MyStringCallback());
    }

    public class MyStringCallback extends StringCallback{

        @Override
        public void onError(Call call, Exception e, int id) {
            Log.e("TAG", "联网失败" + e.getMessage());
        }

        @Override
        public void onResponse(String response, int id) {
            switch (id){
                case 100:
                    if(response != null){
                        processData(response);

                    }
            }

        }
    }

    private void processData(String json) {
        Gson gson = new Gson();
        HotPostBean hotPostBean = gson.fromJson(json, HotPostBean.class);
        result = hotPostBean.getResult();
    }
}
